package controller;

import com.alibaba.fastjson.JSON;
import services.SearchReqDataService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

/**
 * @program: personnel_management_system
 * @description: search page
 * @author: Mr.Sun
 * @create: 2020-05-28 19:47
 **/
@WebServlet("/search")
public class SearchController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受浏览器发送的JSON数据，JSON数据需要先转换成BuffereReader才能读取
        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        System.out.println(json);
        reader.close();
        SearchReqDataService searchRes = new SearchReqDataService();
        Map maps = JSON.parseObject(json,Map.class);
//        System.out.println(maps);

        //取map中的属性在调用service方法
        Integer startAge = (Integer) maps.get("startAge");
        Integer endAge = (Integer) maps.get("endAge");
        Integer startWork = (Integer) maps.get("startWork");
        Integer endWork = (Integer) maps.get("endWork");


        resp.setContentType("text/json;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        try {
            out.println(JSON.toJSONString(searchRes.search(startAge,endAge,startWork,endWork)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }
}
